define(["jquery"],function(){
	return {
		
		dataArr : [],
		currentIndex_r : 0,
		currentIndex_l : 5,
		init : function(){
			
			//初始化显示效果
			this.initStarBox();
			//无缝轮播图的左右滑动
			$(".star_btn_box>.btn_left").on("click",function(){
				this.currentIndexChange(0);
			}.bind(this));
			$(".star_btn_box>.btn_right").on("click",function(){
				this.currentIndexChange(1);
			}.bind(this));
			
		},
		
		initStarBox : function(){
			var that = this;
			$.ajax({
				type:"get",
				url:"data_json/star.json",
				async:true,
				success : function(res){
					
					that.dataArr = res;
					bringItem();
				}
			});
			function bringItem(){
				
				var length = that.dataArr.length;
				var html = "";
				$.each(that.dataArr,function(index,item){
					
					//显示前6条数据
					if(index < 6 ){
						html += `<li class="star_item">
							<a href="javascript:;"><img src="${item.src}"/></a>
							<p class="item_name"><a href="javascript:;">${item.name}</a></p>
							<p class="item_introduce"><a href="javascript:;">${item.introduce}</a></p>
							<p class="item_price"><a href="javascript:;">¥${item.price}</a></p>
						</li>`
					}
					
				});
				
				$(".star_item_container").html(html);
				
			}
				
		},
		
		//
		currentIndexChange : function(type){
				//向左滑动,加载后一条数据
				if(type == 0){
					if(this.currentIndex_l < this.dataArr.length-1){
						this.currentIndex_l++;
					}else{
						this.currentIndex_l = 0;
					}
					var item = this.dataArr[this.currentIndex_l];
					console.log(this.currentIndex_l);
					var $li = $('<li class="star_item"></li>');
					var html = `<a href="javascript:;"><img src="${item.src}"/></a>
							<p class="item_name"><a href="javascript:;">${item.name}</a></p>
							<p class="item_introduce"><a href="javascript:;">${item.introduce}</a></p>
							<p class="item_price"><a href="javascript:;">¥${item.price}</a></p>`;
					$li.html(html);
					$(".star_item_container").append($li).stop(true,false).animate({
						left:-200
					},1000,function(){
						$(".star_item").eq(0).remove();
						$(".star_item_container").css("left",0);
						
					})
				}else{
					//向右滑
					if(this.currentIndex_r > 0){
						this.currentIndex_r--;
					}else{
						this.currentIndex_r = this.dataArr.length-1;
					}
					var item = this.dataArr[this.currentIndex_r];
					console.log(this.currentIndex_r);
					var $li = $('<li class="star_item"></li>');
					var html = `<a href="javascript:;"><img src="${item.src}"/></a>
							<p class="item_name"><a href="javascript:;">${item.name}</a></p>
							<p class="item_introduce"><a href="javascript:;">${item.introduce}</a></p>
							<p class="item_price"><a href="javascript:;">¥${item.price}</a></p>`;
					$li.html(html);
					$(".star_item_container").prepend($li).css("left",-200).stop(true,false).animate({
						left:0
					},1000,function(){
						$(".star_item").last().remove();
					})
				}
				
			}

	}
	
})